﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

using DLTN.Entities;
using DLTN.Data;

public partial class Admin_TinTuc_UploadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!Page.IsPostBack)
        {
            UploadImage.DialogFilter = "Image Files(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF";
            if (Request.QueryString["sFileName"].ToString().Length > 0)
            {
                H_FileChonLai.Value = Request.QueryString["sFileName"].ToString();
            }
        }
    }

    protected void cmdUpload_Click(object sender, EventArgs e)
    {
    }

    public Bitmap resizeImage(System.Drawing.Image img, int width, int height)
    {
        Bitmap b = new Bitmap(width, height);
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);

        g.DrawImage(img, 0, 0, width, height);
        g.Dispose();

        return b;
    }

    protected void UploadImage_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
    {
        string imgNewsPathLon = Server.MapPath(Common.sPathUpTourLon);

        string imgNewsPath = Server.MapPath(Common.sPathUpTour);

        if (!Directory.Exists(imgNewsPath)) Directory.CreateDirectory(imgNewsPath);
        System.Drawing.Image originalIMG = System.Drawing.Image.FromStream(args.OpenStream());
        //Bitmap imgNewThumb = CropSize(originalIMG, 800, 600, AnchorPosition.Center);
        Bitmap imgNewThumb = resizeImage(originalIMG, 300, 150);
        
        //Bitmap imgNewBig = new Bitmap(originalIMG.Width, originalIMG.Height);
        //Bitmap imgNewBigTemp = GetBitMap(imgNewBig, false);

        imgNewThumb = GetBitMap(imgNewThumb, false);
        //imgNewBig = GetBitMap(imgNewBig, false);
        string sFileName = Path.GetFileNameWithoutExtension(args.FileName) + DateTime.Now.ToString("ddMMyyhhmmss") + Path.GetExtension(args.FileName);
        imgNewThumb.Save(imgNewsPath + "\\" + sFileName);
        originalIMG.Save(imgNewsPathLon + "\\" + sFileName);
        imgNewThumb.Dispose();
        originalIMG.Dispose();
        //if (File.Exists(imgNewsPath + "\\" + H_FileChonLai.Value))
        //{
        //    File.Delete(imgNewsPath + "\\" + H_FileChonLai.Value);
        //}
        string strImgInsert = "../../Upload/Tour/" + sFileName;
        string strInsertImg = "<script>";
        strInsertImg = strInsertImg + "parent.parent.Insert_Image('" + strImgInsert + "','" + sFileName + "');";
        strInsertImg = strInsertImg + "parent.parent.GB_hide();";
        strInsertImg = strInsertImg + "</script>";
        Page.RegisterClientScriptBlock("strInsertImg", strInsertImg);
    }

    protected void UploadImage_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs args)
    {
        string imgNewsPath = Server.MapPath(Common.sPathUpTour);
        string sFileName = Path.GetFileNameWithoutExtension(args.Item.FileName) + DateTime.Now.ToString("ddMMyyhhmmss") + Path.GetExtension(args.Item.FileName);
        //if (File.Exists(imgNewsPath + "\\" + sFileName))
        //{
        //    File.Delete(imgNewsPath + "\\" + sFileName);
        //}
    }

    protected Bitmap GetBitMap(Bitmap bitmapTemp, bool insertText)
    {
        Bitmap bmpNew = new Bitmap(bitmapTemp.Width, bitmapTemp.Height);
        Graphics oGraphic = Graphics.FromImage(bmpNew);
        oGraphic.DrawImage(bitmapTemp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmpNew.Width, bmpNew.Height, GraphicsUnit.Pixel);
        bitmapTemp.Dispose();
        bitmapTemp = bmpNew;
        if (insertText)
        {
            SolidBrush oBrush = new SolidBrush(Color.Blue);
            Font oFont = new Font("Arial", 9, FontStyle.Bold);
            oGraphic.DrawString("VYC", oFont, oBrush, 10, 10);
            oFont.Dispose();
            oBrush.Dispose();
        }
        oGraphic.Dispose();
        return bitmapTemp;
    }

    protected Bitmap CropSize(System.Drawing.Image originalIMG, int LWidth, int LHeight, AnchorPosition Anchor)
    {
        int targetH = 0;
        int targetW = 0;
        if (originalIMG.Height > originalIMG.Width)
        {
            targetH = LHeight;
            targetW = Convert.ToInt32(originalIMG.Width * ((float)targetH / (float)originalIMG.Height));
        }
        else
        {
            targetW = LWidth;
            targetH = Convert.ToInt32(originalIMG.Height * ((float)targetW / (float)originalIMG.Width));
        }
        Bitmap bmPhoto = new Bitmap(LWidth, LHeight, PixelFormat.Format24bppRgb);
        try
        {
            bmPhoto.SetResolution(72, 72);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            try
            {
                grPhoto.SmoothingMode = SmoothingMode.HighQuality;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                int newx = 0;
                int newy = 0;
                if (targetW < LWidth)
                {
                    newx = (LWidth - targetW) / 2;
                }
                if (targetH < LHeight)
                {
                    newy = (LHeight - targetH) / 2;
                }
                grPhoto.FillRectangle(System.Drawing.Brushes.White, 0, 0, LWidth, LHeight);
                grPhoto.DrawImage(originalIMG, new Rectangle(newx, newy, targetW, targetH), 0, 0, originalIMG.Width, originalIMG.Height, GraphicsUnit.Pixel);
            }
            catch (Exception) { }
            finally
            {
                grPhoto.Dispose();
            }
        }
        catch (Exception)
        {
        }
        return bmPhoto;
    }

    public enum AnchorPosition
    {
        Top,
        Center,
        Bottom,
        Left,
        Right
    }
}
